//
//  LoginTextField.swift
//  AESOLAR_IOS
//
//  Created by Mr.Wang on 2019/8/19.
//  Copyright © 2019 Mr.Wang. All rights reserved.
//

import UIKit
import RxSwift
import RxCocoa

class LoginTextField: UIView {

    let imageView = UIImageView()
    let textField = UITextField()
    let optionalLabel = UILabel()
    
    var placeHolder: String {
        set {
            textField.placeholder = newValue
        }
        get {
            return textField.placeholder ?? ""
        }
    }
    
    var image: UIImage? {
        set {
            imageView.image = newValue
        }
        get {
            return imageView.image
        }
    }
    
    var text: String {
        set {
            textField.text = newValue
        }
        get {
            return textField.text ?? ""
        }
    }
    
    var optional: Bool {
        set {
            optionalLabel.isHidden = !newValue
        }
        get {
            return !optionalLabel.isHidden
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        imageView.contentMode = .scaleAspectFit
        self.addSubview(imageView)
        imageView.snp.makeConstraints { (make) in
            make.left.equalTo(0)
            make.centerY.equalToSuperview()
            make.width.height.equalTo(20)
        }
        
        textField.placeholder = ""
        textField.font = UIFont.systemFont(ofSize: 14, weight: .medium)
        self.addSubview(textField)
        textField.snp.makeConstraints { (make) in
            make.left.equalTo(imageView.snp.right).offset(10)
            make.top.bottom.equalToSuperview()
            make.right.equalTo(0)
        }
        
        let sepator = UIView()
        sepator.backgroundColor = #colorLiteral(red: 0.9176470588, green: 0.9176470588, blue: 0.9176470588, alpha: 1)
        self.addSubview(sepator)
        sepator.snp.makeConstraints { (make) in
            make.left.bottom.right.equalToSuperview()
            make.height.equalTo(1)
        }
        
        optionalLabel.isHidden = true
        optionalLabel.textColor = #colorLiteral(red: 0.8274509804, green: 0.8274509804, blue: 0.8274509804, alpha: 1)
        optionalLabel.text = "非必填".localized
        optionalLabel.font = UIFont.systemFont(ofSize: 14, weight: .medium)
        self.addSubview(optionalLabel)
        optionalLabel.snp.makeConstraints { (make) in
            make.right.equalTo(-15)
            make.centerY.equalToSuperview()
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension Reactive where Base: LoginTextField {
    
    var text: ControlProperty<String?> {
        return base.textField.rx.text
    }
}

extension UIConfig where ViewType: LoginTextField {
    
    @discardableResult
    func placeHolder(_ placeHolder: String) -> UIConfig<ViewType> {
        view.placeHolder = placeHolder
        return self
    }
    
    @discardableResult
    func image(_ image: UIImage?) -> UIConfig<ViewType> {
        view.image = image
        return self
    }
    
}
